Issue using Python to solve the Coin problem [closed]
Posted
by challarao
on Stack Overflow
See other posts from Stack Overflow
or by challarao
Published on 2010-06-17T05:05:53Z
Indexed on
2010/06/17
5:43 UTC
Read the original article
Hit count: 190
python
|diophantine
I'm attempting to solve a problem commonly known as the Coin problem, but using McNuggets. McNuggets come in boxes containing either 6, 9, or 20 nuggets. I want to write a python script that uses Diophantine equations to determine if a given number of McNuggets n
can be exactly purchased in these groupings. For example:
- 26 McNuggets -- Possible: 1 6-pack, 0 9-packs, 1 20-pack
- 27 McNuggets -- Possible: 0 6-packs, 3 9-packs, 0 20-packs
- 28 McNuggets -- Not possible
This is my current attempt at writing the solution in Python, but the output is incorrect and I'm not sure what's wrong.
n=input("Enter the no.of McNuggets:")
a,b,c=0,0,0
count=0
for a in range(n):
if 6*a+9*b+20*c==n:
count=count+1
break
else:
for b in range(n):
if 6*a+9*b+20*c==n:
count=count+1
break
else:
for c in range(n):
if 6*a+9*b+20*c==n:
count=count+1
break
if count>0:
print "It is possible to buy exactly",n,"packs of McNuggetss",a,b,c
else:
print "It is not possible to buy"
© Stack Overflow or respective owner